package cn.test.thread;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 * @author Han
 */
public class ReadBook {
	public static void main(String[] args) throws MalformedURLException, IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter("c:/1.txt",true));
		String url = "http://www.jdxs.net/files/article/html/94/94173/";
		Document d = Jsoup.parse(new URL(url), 10000);
		Element table = d.getElementById("Table1");
		Elements links = table.getElementsByTag("a");
		for(Iterator<Element> it = links.iterator();it.hasNext();){
			String link = it.next().attr("href");
			if(link.contains("http") && !link.endsWith("html")){
				continue;
			}
			System.out.println(link);
			Document doc = Jsoup.parse(new URL(url+link), 10000);
			Element content = doc.getElementsByClass("content").first();
			String tmp = content.toString().replace("<br>", "@newline@");
			tmp = tmp.substring(0,tmp.indexOf("<div class=\"con_l\">"));
			doc = Jsoup.parse(tmp);
			String part = doc.text().replace("@newline@", "\r\n");
			bw.write(part);
			bw.write("\r\n\r\n");
		}
		bw.flush();
	}
}
